<?php
require_once '../libs/Smarty.class.php';

$smarty = new Smarty ();

//$smarty->force_compile = true;
//$smarty->debugging = true;
$smarty->caching = false;
//$smarty->cache_lifetime = 120;
session_start ();
if ($_SERVER ["REQUEST_METHOD"] == "POST") {
	//POST method : user post a login form
	if (isset ( $_POST ["username"] ) && isset ( $_POST ["password"] )) {
		$username = $_POST ["username"];
		$password = $_POST ["password"];
		require_once '../util/auth.php';
		if (tryLogin ( $username, $password )) {
			header ( 'Location: /cpanel/index.php' );
		}
		else{
			$smarty->assign("msgTitle", "错误");
			$smarty->assign("message", "登录失败(用户名或密码错误)");
			$smarty->display("../info_back.tpl");
		}
	
	} else {
		die ( "Need username and password." );
	}
} else {
	//GET method
	require_once '../util/auth.php';
	if (isLoggedIn ()) {
		header ( 'Location: /cpanel/index.php' );
	} else {
		$smarty->display ( '../cpanel/login.tpl' );
	}
}
